
function toTree(list, parent,id,pid) {
  const children = list.filter(item => {
    return item[pid] === parent[id];
  });
  if (children.length !== 0) {
    parent.children = children;
    children.forEach(item => {
      toTree(list, item,id,pid);
    });
  }
}

function getParent(list,id,pid) {
  return list.filter(item => {
    return list.every(obj => {
      return item[pid] !== obj[id];
    });
  });
}

export function list2tree(list,id='id',pid='parentId'){
  const topList = getParent(list,id,pid);
  topList.forEach(item => toTree(list, item,id,pid));
  return topList;
}

